﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using SystemScanningGui.ScanService;
using System.Data.Services.Client;

namespace SystemScanningGui.ViewModels
{
	class ScanComparisonViewModel : GalaSoft.MvvmLight.ViewModelBase
	{
		#region Fields 

		private string _LeftHeader = "";
		private string _RightHeader = "";

		#endregion Fields 

		#region Properties 

		public IEnumerable<ViewModels.Difference> Differences
		{
			get;
			private set;
		}

		public string LeftHeader 
		{
			get
			{
				return _LeftHeader;
			}
			set
			{
				_LeftHeader = value;
			}
		}

		public string RightHeader
		{
			get
			{
				return _RightHeader;
			}
			set
			{
				_RightHeader = value;
			}
		}

		#endregion Properties 

		#region Methods 

		// Public Methods 

		public void Initialize(Scan scanLeft, 
            Scan scanRight, 
			Computer computerLeft,
			Computer computerRight,
            CoreEntities _entities)
		{
			LeftHeader = string.Format("{1}{0}{2:yy-MM-dd HH:mm}", Environment.NewLine, computerLeft.Hostname, scanLeft.Performed);
			RightHeader = string.Format("{1}{0}{2:yy-MM-dd HH:mm}", Environment.NewLine, computerRight.Hostname, scanRight.Performed);

			Differences = null;

			var systemStateQuery = from s in _entities.ConsolidatedSystemStates
								   where (s.ScanID == scanLeft.ScanID
								   || s.ScanID == scanRight.ScanID)
								   select s;

			var dsq = systemStateQuery as DataServiceQuery<ConsolidatedSystemState>;

			AsyncCallback foo = (asyncState) =>
			{
				var query = asyncState.AsyncState as DataServiceQuery<ConsolidatedSystemState>;
				var systemState = query.EndExecute(asyncState).ToList();

				var leftNotOnRight =
					from ssL in systemState
					where systemState.Any(ssR => 
						ssR.ComparisonIdentifier == ssL.ComparisonIdentifier
						&& ssR.ScanID == scanRight.ScanID
						) == false
						&& ssL.ScanID == scanLeft.ScanID
					select ssL;
				var rightNotOnLeft =
					from ssR in systemState
					where systemState.Any(ssL => 
						ssR.ComparisonIdentifier == ssL.ComparisonIdentifier
						&& ssL.ScanID == scanLeft.ScanID
						) == false
						&& ssR.ScanID == scanRight.ScanID
					select ssR;


				var diffs = new List<Difference>();

				foreach (var item in leftNotOnRight)
				{
					diffs.Add(new Difference
					{
						ExistsOnLeft = true,
						ExistsOnRight = false,
						LeftVisibility = System.Windows.Visibility.Visible,
						RightVisibility = System.Windows.Visibility.Collapsed,
						Description = item.ObjectDescription,
						Identifier = item.ComparisonIdentifier,
						Type = item.ObjectType,
						FromWhere = item.Hostname
					});
				}

				foreach (var item in rightNotOnLeft)
				{
					diffs.Add(new Difference
					{
						ExistsOnRight = true,
						ExistsOnLeft = false,
						LeftVisibility = System.Windows.Visibility.Collapsed,
						RightVisibility = System.Windows.Visibility.Visible,
						Description = item.ObjectDescription,
						Identifier = item.ComparisonIdentifier,
						Type = item.ObjectType,
						FromWhere = item.Hostname
					});
				}

				Differences = diffs;
				this.RaisePropertyChanged("Differences");
			};

			dsq.BeginExecute(foo, dsq);
			this.RaisePropertyChanged("LeftHeader");
			this.RaisePropertyChanged("RightHeader");
		}

		#endregion Methods 
	}
}
